﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Admin
{
    public partial class AddForum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                GetForumGroup();

            }
        }

        private void BindData()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forum = db_connect.Forums.OrderByDescending(f => f.forumidPK).ToList();
                dgforumlist.DataSource = forum;
                dgforumlist.DataBind();
            }
        }


        private void GetForumGroup()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forumgroup = db_connect.ForumGroups.OrderBy(f => f.forumgroupidPK).ToList();
                dpforumgroup.DataSource = forumgroup;
                dpforumgroup.DataTextField = "forumgroupname";
                dpforumgroup.DataValueField = "forumgroupidPK";
                dpforumgroup.DataBind();

            }
        }

        protected void btnadd_Click(object sender, EventArgs e)
        {
            if (dpforumgroup.SelectedIndex == -1)
            {

            }
            else
            {
                using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
                {
                    DAL.Forum forum = new DAL.Forum();
                    forum.forumtitle = txtforumtitle.Text.Trim();
                    forum.forumdescription = txtforumdescription.Text.Trim();
                    forum.forumgroupidFK = Convert.ToInt32(dpforumgroup.SelectedValue);
                    forum.datecreated = DateTime.Now;
                    forum.disabled = false;
                    db_connect.Forums.Add(forum);
                    db_connect.SaveChanges();
                    BindData();
                }
            }
        }

        protected void dgforumlist_EditCommand(object sender, DataGridCommandEventArgs e)
        {
            dgforumlist.EditItemIndex = e.Item.ItemIndex;
            BindData();
        }

        protected void dgforumlist_Cancel(object sender, DataGridCommandEventArgs e)
        {
            dgforumlist.EditItemIndex = -1;
            BindData();
        }

        protected void dgforumlist_Update(object sender, DataGridCommandEventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                TextBox forumgrouptitle = (TextBox)dgforumlist.Items[e.Item.ItemIndex].FindControl("txtforumtitle");
                TextBox forumdescription = (TextBox)dgforumlist.Items[e.Item.ItemIndex].FindControl("txtdescription");
                CheckBox disabled = (CheckBox)dgforumlist.Items[e.Item.ItemIndex].FindControl("cbenabled");
              
                Label forumgroupidPK = (Label)dgforumlist.Items[e.Item.ItemIndex].FindControl("lbforumid_");
                int forumid = int.Parse(forumgroupidPK.Text);
                var forumgroup = db_connect.Forums.Where(f => f.forumidPK.Equals(forumid)).SingleOrDefault();
                forumgroup.disabled = disabled.Checked;
                forumgroup.forumtitle = forumgrouptitle.Text.Trim();
                forumgroup.forumdescription = forumdescription.Text.Trim();
                db_connect.SaveChanges();
                dgforumlist.EditItemIndex = -1;
                BindData();
            }
        }

        protected void dgforumlist_Paging(object sender, DataGridPageChangedEventArgs e)
        {
            dgforumlist.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }

        private void UpdateForumGroup(int forumgroupid)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {

            }
        }




    }
}